<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <th:block th:include="include :: header('模版编辑器')"/>
  <th:block th:include="include :: codemirror"/>
  <!-- 引入css文件，用以支持主题 -->
  <link rel="stylesheet" th:href="@{'/ajax/libs/codemirror/theme/' + ${@config.getKey('gen_preview_theme')} + '.css'}"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
  <textarea id="editor">[[${templateContent}]]</textarea>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
  var previewTheme = [[${@config.getKey('gen_preview_theme')}]];
  var prefix = ctx + "tool/template";

  var editor;

  $(function () {
    // 根据DOM元素的id构造出一个编辑器
    editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
      lineNumbers: true,     // 显示行数
      styleActiveLine: true, // 当前行背景高亮
      matchBrackets: true,   // 括号匹配
      mode: {name: "xml", alignCDATA: true},
      mode: {name: "html5", alignCDATA: true},
      mode: {name: "javadoc", alignCDATA: true},
      mode: {name: "java", alignCDATA: true},
      mode: {name: "javascript", alignCDATA: true},
      lineWrapping: false,   // 自动换行默认true
      foldGutter: true,      // 支持代码折叠
      theme: previewTheme,	     // 设置主题
    });
    // 设置需要初始化的代码内容
    editor.setValue($("#editor").text());
    editor.setSize('100%', '415px');

  })

  function submitHandler(callback) {
    // 同步CodeMirror的数据到textarea
    editor.save();
    $.operate.submit(prefix + "/editor", 'post', 'json', {
      'templateId': [[${templateId}]],
      'templateContent': $("#editor").val()
    }, function (result) {
      if (result.code === web_status.SUCCESS) {
        $.modal.msgSuccess("保存成功");
      } else if (result.code === web_status.WARNING) {
        $.modal.alertWarning(result.msg);
      } else {
        $.modal.alertError(result.msg);
      }
      callback();
    });
  }

  //转意符换成普通字符
  function escape2Html(str) {
    var arrEntities = {'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"'};
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
      return arrEntities[t];
    });
  }

  //普通字符转换成转意符
  function html2Escape(sHtml) {
    return sHtml.replace(/[<>&"]/g, function (c) {
      return {'<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;'}[c];
    });
  }

</script>
</body>
</html>